package com.oamicnet.mapstruct.service.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 新增请求头
        ServerHttpRequest request = exchange.getRequest().mutate()
                .header("User-Type", "admin") // 这里添加自定义请求头
                .build();

        // 用新的 request 构建新的 exchange
        ServerWebExchange newExchange = exchange.mutate().request(request).build();

        return chain.filter(newExchange);
    }
    @Override
    public int getOrder() {
        return 0; // 数字越小优先级越高
    }
}
